package com.power.sifa.net.controller;

import com.plf.common.domain.ResponseDTO;
import com.plf.common.exception.BusinessException;
import com.plf.core.base.service.Ha_formService;
import com.power.sifa.approve.service.Apl_matter_attachService;
import com.power.sifa.net.api.IApplyFeign;
import com.power.sifa.net.model.Form_apply;
import com.power.sifa.net.model.enumeration.Tp_matter;
import com.power.sifa.net.service.ApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController
public class ApplyController implements IApplyFeign {

    @Resource
    private ApplyService applyService;
    @Resource
    private Ha_formService ha_formService;
    @Resource
    private Apl_matter_attachService apl_matter_attachService;

    @Override
    public ResponseDTO saveApplyForm(String formid, Form_apply form_apply) throws Exception {
        Assert.notNull(formid, "未传入表单id");
        Assert.notNull(form_apply.getApplyid(), "未传入申请表id");
        Assert.notNull(form_apply.getMatid(), "未传入事项id");
        this.applyService.saveApplyForm(formid, form_apply);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO fixedSaveApplyForm(String formid, Form_apply form_apply) {
        try {
            this.applyService.saveApplyForm(formid, form_apply);
            return ResponseDTO.succ();
        } catch (Exception e) {
            log.error("fixedSaveApplyForm 定时保存失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.succ();
        }
    }

    @Override
    public ResponseDTO loadApplyForm(String formid, String applyid) throws Exception {
        return ResponseDTO.succData(this.applyService.loadApplyForm(formid, applyid));
    }

    @Override
    public ResponseDTO flowLoadForm(Map<String, Object> struct) throws Exception {
        Object result = this.ha_formService.loadFormData(struct);
        return ResponseDTO.succData(result);
    }

    @Override
    public ResponseDTO submitApply(String applyid, String matid) throws BusinessException {
        // 1. 校验材料
        // 1.1 人员布控没有材料跳过
        if(!Tp_matter.PUB_CORRECTION.equals(matid)) {
            ResponseDTO result = this.apl_matter_attachService.check(applyid);
            if (0 != result.getCode()) {
                return result;
            }
        }

        // 2. 提交申请
        this.applyService.submitApply(applyid);
        return ResponseDTO.succ();
    }

}
